/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EJERCICIO3;

/**
 *
 * @author emperatriz
 */
public class AppMemoriaInterna {
    public static void  EscribirArregloMemoria(int[] a)       
    {
        for(int i=0;i<a.length;i++)
        {
             
                if(i==0)
                    System.out.print("[" +a[i]+ ", ");
                else
                {
                if(i<(a.length -1))
                    System.out.print( a[i]+ ", ");
                else
                    System.out.println(a[i]+ "]");
                }
        }
        
    }
       public static void EscribirArregloMatriz(String[][] a, int NF, int NC)
        {
            for (int i = 0; i < NF; i++)
            {
                for (int j = 0; j < NC; j++)
                {
                    System.out.print("[" + a[i ][j] + ", ");
                    if (i == (NC - 1))
                       System.out.println(a[i][ j] + "] ");
                }
            }

        }

    public static void LRU(int [] a,int n)
    {
    int []Memoria=new int[n];
    for(int j=0;j<a.length;j++)
    {

             Memoria[j % n]= j;
             EscribirArregloMemoria(Memoria);   
    }
    }
    public static void MRU(int [] DD, int n)
    {
        int []Memoria=new int[n];
        int j=0;
        for(int i=0;i<DD.length;i++)
        {
             if(i>n-1)
             {
                 j=n-1;
                 Memoria[j]= i;
                 EscribirArregloMemoria(Memoria);  
             }
             if(i<n)
             {
             Memoria[i]= i;
              EscribirArregloMemoria(Memoria); 
             }
             
             
         }
    }
 public static void BloquesClavados(int[] DD, int n, int NB)
    {
            int j = 0;
            int[] Memoria = new int[n];
            for (int i = 0; i < DD.length; i++)
            {
                if (i >= n )//cuando la memoria se lleno
                {

                    if (((j % n) + NB) >= n )
                    {
                        j = 0;
                    }


                    Memoria[(j % n) + (NB)] = i;

                    EscribirArregloMemoria(Memoria);
                } 
                if (i < n)
                {
                    Memoria[i] = i;
                    EscribirArregloMemoria(Memoria);
                }
                j = j + 1;
            }

        }
        
 
  public static String[][] LRU1(String[][] a, int NF, int NC, String[][] b, int NF1, int NC1, int n)
        {
            int[] Memoria = new int[n];
           String[][] R = new String[NF*NF1][NC+NC1];
            for(int i=0;i<NF;i++)
            {
                for (int j = 0; j <NC1 ; j++)
                {
                    if (a[i][0] == b[j][1])
                    {
                        R[i][0] = a[i][0];
                        R[i][1] = a[i][1];
                        R[i][2] = a[i][2];
                        R[i][ 3] = b[i][0];
                        R[i][4] = b[i][ 1];
                        R[i][5] = b[i][2];
                    }

 
                }
            }

            return R;
            }
        public static void main(String[] args)
          {
               //int [] M=new int [9];//ARREGLO DE REGISTROS 
              //LRU(M,5);
              //MRU(M,5);
             // BloquesClavados(M,5,2);
            int FC=4;
            int CC=3;
            int FP=4;
            int CP=3;
            String [][]C=new String[FC] [CC];//Cliente
            C[0][0] ="103177";
            C[0][1] = "Juan Vargas Llosa";
            C[0][2] = "Cusco";
            C[1][0] = "111111";
            C[1][ 1] = "Berta Yucra Amao";
            C[1][ 2] = "Apurimac";
            C[2][0] = "202020";
            C[2][ 1] = "Laura Domingues Peres";
            C[2][ 2] = "Lima";
            C[3][ 0] = "303030";
            C[3][ 1] = "Mario Quispe Chavez";
            C[3][ 2] = "Lima";
            String[][]P = new String[FP][CP];//PRESTAMO
            P[0][0] = "P0001";
            P[0][1] = "103177";
            P[0][2] = "4000";
            P[1][ 0] = "P0002";
            P[1][1] = "103177";
            P[1][2] = "500";
            P[2][0] = "P0003";
            P[2][1] = "111111";
            P[2][ 2] = "600";
            P[3][0] = "P0004";
            P[3][ 1] = "303030";
            P[3][2] = "700";
              
              //
             String[][] R = new String[10][CC + CP];

            R = LRU1(C,FC,CC,P,FP,CP,5);
            EscribirArregloMatriz(R, R.length, (CC + CP));
         }
}
